package com.example.kleinanzeigengenerator.kamera;

import java.io.File;

import com.example.kleinanzeigengenerator.common.Constants;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;

public class HandyKamera extends Activity{
	private boolean done = true;
	File sdDir;

	protected static final String PHOTO_TAKEN = "photo_taken";

	@Override
	public void onCreate(Bundle savedInstanceState) {

		try {
			super.onCreate(savedInstanceState);         
			File root = new File(Environment.getExternalStorageDirectory() + File.separator + "kleinanzeigengenerator" + File.separator);
			root.mkdirs();
			sdDir = new File(root, "ichBinEineBohne.jpg");
			Log.d(Constants.HANDY_KAMERA_TAG, "Storage File: " + sdDir.getPath());
			startCameraActivity();
		} catch (Exception e) {
			finish();
		}

	}

	protected void startCameraActivity() {
		Uri outputFileUri = Uri.fromFile(sdDir);
		Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
		intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
		startActivityForResult(intent, 0);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Intent intent = new Intent();
		intent.putExtra("uri", sdDir.getPath());
		setResult(Constants.WERBINICH_TAG, intent);
		finish();
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		if (savedInstanceState.getBoolean(HandyKamera.PHOTO_TAKEN)) {
			done = true;
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putBoolean(HandyKamera.PHOTO_TAKEN,  done);
	}


}